import 'package:flutter/material.dart';
import 'package:mcdonalds/lblbc_ui_kit.dart';
import 'package:mcdonalds/network/bean/query_order_list_resp_entity.dart';
import 'package:mcdonalds/network/http_manager.dart';
import 'package:mcdonalds/pages/order/order_detail.dart';

class OrderListPage extends StatefulWidget {
  final int orderStatus;

  const OrderListPage(this.orderStatus, {Key? key}) : super(key: key);

  @override
  createState() => _OrderListState();
}

class _OrderListState extends State<OrderListPage> {
  int currentTab = 0;

  @override
  void initState() {
    super.initState();
    currentTab = widget.orderStatus;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的订单', style: TextStyle(color: Colors.white)),
      ),
      body: OrderListWidget(),
    );
  }
}

class OrderListWidget extends StatefulWidget {
  const OrderListWidget({Key? key}) : super(key: key);

  @override
  _OrderListWidgetState createState() {
    return _OrderListWidgetState();
  }
}

class _OrderListWidgetState extends State<OrderListWidget> {
  List<QueryOrderListRespData> _dataList = [];

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: _dataList.map((QueryOrderListRespData queryOrderListRespData) {
          return Card(
            color: Colors.white,
            child: Container(
              padding: const EdgeInsets.all(10),
              child: InkWell(
                child: buildOrderBlock(queryOrderListRespData),
                onTap: () {
                  gotoOrderDetailPage(queryOrderListRespData.orderId);
                },
              ),
            ),
          );
        }).toList(),
      ),
    );
  }

  Column buildOrderBlock(QueryOrderListRespData queryOrderListRespData) {
    return Column(
      children: [
        Column(
          children: queryOrderListRespData.list.map((QueryOrderListRespDataList queryOrderListRespDataItem) {
            return Column(
              children: [
                Row(
                  children: [
                    Image(
                      image: NetworkImage(queryOrderListRespDataItem.squarePic),
                      width: 100,
                      height: 100,
                    ),
                    lblRowSpacer(10),
                    Expanded(
                        child: Text(queryOrderListRespDataItem.name, maxLines: 2, overflow: TextOverflow.ellipsis)),
                    Column(
                      children: [
                        Text("￥${queryOrderListRespDataItem.price / 100}"),
                        Text("×${queryOrderListRespDataItem.quantity}")
                      ],
                    )
                  ],
                ),
              ],
            );
          }).toList(),
        ),
        lblColumnSpacer(10),
        lblDivider(),
        Row(
          children: [
            Spacer(),
            OutlinedButton(
              child: const Text('删除订单', style: TextStyle(color: Color(0xFF575E64))),
              onPressed: () {},
            ),
          ],
        )
      ],
    );
  }

  @override
  void initState() {
    super.initState();
    _queryData();
  }

  _queryData() async {
    String url = "mcdonalds/orders?orderStatus=0";
    HttpManager.getInstance().get(url).then((resp) {
      var result = QueryOrderListRespEntity.fromJson(resp);
      setState(() {
        _dataList = result.data;
      });
    });
  }

  void gotoOrderDetailPage(String orderId) {
    Navigator.push(context, MaterialPageRoute(builder: (context) => OrderDetailPage(orderId)))
        .then((value) => {_queryData()});
  }
}

class OrderTab {
  int status = 0;
  String name = "";

  OrderTab(this.status, this.name);
}
